﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TestTableDataExtractionInstruction:InstructionBase 
    {
        #region props
        private string _CorrectResultConnStr;
        /// <summary></summary>
        public string CorrectResultConnStr
        {
            get
            {
                return this._CorrectResultConnStr;
            }
            set
            {
                this._CorrectResultConnStr = value;
            }
        }

        private string _CorrectResultSelectSql;
        /// <summary></summary>
        public string CorrectResultSelectSql
        {
            get
            {
                return this._CorrectResultSelectSql;
            }
            set
            {
                this._CorrectResultSelectSql = value;
            }
        }

        private string _CorrectResultDocIDField;
        /// <summary></summary>
        public string CorrectResultDocIDField
        {
            get
            {
                return this._CorrectResultDocIDField;
            }
            set
            {
                this._CorrectResultDocIDField = value;
            }
        }

        private string _CorrectResultPageNumberField;
        /// <summary></summary>
        public string CorrectResultPageNumberField
        {
            get
            {
                return this._CorrectResultPageNumberField;
            }
            set
            {
                this._CorrectResultPageNumberField = value;
            }
        }

        private string _OurDocListResultFilePath;
        /// <summary></summary>
        public string OurDocListResultFilePath
        {
            get
            {
                return this._OurDocListResultFilePath;
            }
            set
            {
                this._OurDocListResultFilePath = value;
            }
        }

        private string _OurTableScanResultFilePath;
        /// <summary></summary>
        public string OurTableScanResultFilePath
        {
            get
            {
                return this._OurTableScanResultFilePath;
            }
            set
            {
                this._OurTableScanResultFilePath = value;
            }
        }

        private string _OurTableLayoutResultFilePath;
        /// <summary></summary>
        public string OurTableLayoutResultFilePath
        {
            get
            {
                return this._OurTableLayoutResultFilePath;
            }
            set
            {
                this._OurTableLayoutResultFilePath = value;
            }
        }

        private string _OurTableExtractResultFolderPath;
        /// <summary></summary>
        public string OurTableExtractResultFolderPath
        {
            get
            {
                return this._OurTableExtractResultFolderPath;
            }
            set
            {
                this._OurTableExtractResultFolderPath = value;
            }
        }

        private string _DocListPerformanceResultFilePath;
        /// <summary></summary>
        public string DocListPerformanceResultFilePath
        {
            get
            {
                return this._DocListPerformanceResultFilePath;
            }
            set
            {
                this._DocListPerformanceResultFilePath = value;
            }
        }

        private string _TableScanPerformanceResultFilePath;
        /// <summary></summary>
        public string TableScanPerformanceResultFilePath
        {
            get
            {
                return this._TableScanPerformanceResultFilePath;
            }
            set
            {
                this._TableScanPerformanceResultFilePath = value;
            }
        }

        private string _TableLayoutPerformanceResultFilePath;
        /// <summary></summary>
        public string TableLayoutPerformanceResultFilePath
        {
            get
            {
                return this._TableLayoutPerformanceResultFilePath;
            }
            set
            {
                this._TableLayoutPerformanceResultFilePath = value;
            }
        }

        private string _TableExtractPerformanceResultFilePath;
        /// <summary></summary>
        public string TableExtractPerformanceResultFilePath
        {
            get
            {
                return this._TableExtractPerformanceResultFilePath;
            }
            set
            {
                this._TableExtractPerformanceResultFilePath = value;
            }
        }

        private Dictionary<string, string> _SrcTgtFieldMappings;
        /// <summary></summary>
        public Dictionary<string, string> SrcTgtFieldMappings
        {
            get
            {
                return this._SrcTgtFieldMappings;
            }
            set
            {
                this._SrcTgtFieldMappings = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public TestTableDataExtractionInstruction():base(Guid.Empty, Guid.Empty)
        {
            this._CorrectResultConnStr = string.Empty;
            this._CorrectResultSelectSql = string.Empty;
            this._CorrectResultDocIDField = string.Empty;
            this._CorrectResultPageNumberField = string.Empty;

            this._OurDocListResultFilePath = string.Empty;
            this._OurTableScanResultFilePath = string.Empty;
            this._OurTableLayoutResultFilePath = string.Empty;
            this._OurTableExtractResultFolderPath = string.Empty;

            this._DocListPerformanceResultFilePath = string.Empty;
            this._TableScanPerformanceResultFilePath = string.Empty;
            this._TableLayoutPerformanceResultFilePath = string.Empty;
            this._TableExtractPerformanceResultFilePath = string.Empty;

            this._SrcTgtFieldMappings = new Dictionary<string, string>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="correctResultConnStr"></param>
        /// <param name="correctResultDocIDField"></param>
        /// <param name="correctResultPageNumberField"></param>
        /// <param name="correctResultSelectSql"></param>
        /// <param name="ourDocListResultFilePath"></param>
        /// <param name="ourTableExtractResultFolderPath"></param>
        /// <param name="ourTableLayoutResultFilePath"></param>
        /// <param name="ourTableScanResultFilePath"></param>
        /// <param name="docListPerformanceResultFilePath"></param>
        /// <param name="tableScanPerformanceResultFilePath"></param>
        /// <param name="tableLayoutPerformanceResultFilePath"></param>
        /// <param name="tableExtractPerformanceResultFilePath"></param>
        /// <param name="srcTgtFieldMappings"></param>
        public TestTableDataExtractionInstruction(Guid componentID, Guid jobTicketID, 
            string correctResultConnStr, string correctResultDocIDField, 
            string correctResultPageNumberField, string correctResultSelectSql, 
            string ourDocListResultFilePath, string ourTableExtractResultFolderPath, 
            string ourTableLayoutResultFilePath, string ourTableScanResultFilePath,
            string docListPerformanceResultFilePath, string tableScanPerformanceResultFilePath,
            string tableLayoutPerformanceResultFilePath, string tableExtractPerformanceResultFilePath,
            Dictionary<string,string> srcTgtFieldMappings) 
            : base(componentID, jobTicketID)
        {
            this._CorrectResultConnStr = correctResultConnStr;
            this._CorrectResultDocIDField = correctResultDocIDField;
            this._CorrectResultPageNumberField = correctResultPageNumberField;
            this._CorrectResultSelectSql = correctResultSelectSql;
            this._OurDocListResultFilePath = ourDocListResultFilePath;
            this._OurTableExtractResultFolderPath = ourTableExtractResultFolderPath;
            this._OurTableLayoutResultFilePath = ourTableLayoutResultFilePath;
            this._OurTableScanResultFilePath = ourTableScanResultFilePath;
            this._DocListPerformanceResultFilePath = docListPerformanceResultFilePath;
            this._TableScanPerformanceResultFilePath = tableScanPerformanceResultFilePath;
            this._TableLayoutPerformanceResultFilePath = tableLayoutPerformanceResultFilePath;
            this._TableExtractPerformanceResultFilePath = tableExtractPerformanceResultFilePath;
            this._SrcTgtFieldMappings = srcTgtFieldMappings;
        }
        #endregion

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                TestTableDataExtractionInstruction instruction = new TestTableDataExtractionInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.CorrectResultConnStr = XmlDataUtil.GetAttributeValue(xNode, "CorrectResultConnectionString",
                                                                                 "");
                instruction.CorrectResultDocIDField = XmlDataUtil.GetAttributeValue(xNode, "CorrectResultDocIDField", "");
                instruction.CorrectResultSelectSql = XmlDataUtil.GetAttributeValue(xNode, "CorrectResultSelectSql", "");
                instruction.CorrectResultPageNumberField = XmlDataUtil.GetAttributeValue(xNode,
                                                                                         "CorrectResultPageNumberField",
                                                                                         "");

                instruction.OurDocListResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "OurDocListResultFilePath",
                                                                                     "");
                instruction.OurTableScanResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                       "OurTableScanResultFilePath", "");
                instruction.OurTableLayoutResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                         "OurTableLayoutResultFilePath",
                                                                                         "");
                instruction.OurTableExtractResultFolderPath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                            "OurTableExtractResultFolderPath",
                                                                                            "");

                instruction.DocListPerformanceResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                             "DocListPerformanceResultFilePath",
                                                                                             "");
                instruction.TableScanPerformanceResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                               "TableScanPerformanceResultFilePath",
                                                                                               "");
                instruction.TableLayoutPerformanceResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                                 "TableLayoutPerformanceResultFilePath",
                                                                                                 "");
                instruction.TableExtractPerformanceResultFilePath = XmlDataUtil.GetAttributeValue(xNode,
                                                                                                  "TableExtractPerformanceResultFilePath",
                                                                                                  "");


                XmlNodeList mappingNodes = xNode.SelectNodes("FieldMappings/FieldMapping");
                if (mappingNodes != null && mappingNodes.Count > 0)
                {
                    instruction.SrcTgtFieldMappings = new Dictionary<string, string>();
                    foreach (XmlNode mappingNode in mappingNodes)
                    {
                        string srcFieldName = XmlDataUtil.GetAttributeValue(mappingNode, "FieldNameInFile", "");
                        if (!instruction.SrcTgtFieldMappings.ContainsKey(srcFieldName))
                        {
                            string tgtFieldName = XmlDataUtil.GetAttributeValue(mappingNode, "FieldNameInTable", "");
                            instruction.SrcTgtFieldMappings.Add(srcFieldName, tgtFieldName);
                        }
                    }
                }

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CorrectResultConnectionString", this.CorrectResultConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CorrectResultSelectSql", this.CorrectResultSelectSql);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CorrectResultPageNumberField",
                                        this.CorrectResultPageNumberField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CorrectResultDocIDField", this.CorrectResultDocIDField);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OurDocListResultFilePath", this.OurDocListResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OurTableScanResultFilePath", this.OurTableScanResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OurTableLayoutResultFilePath",
                                        this.OurTableLayoutResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OurTableExtractResultFolderPath",
                                        this.OurTableExtractResultFolderPath);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocListPerformanceResultFilePath",
                                        this.DocListPerformanceResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableScanPerformanceResultFilePath",
                                        this.TableScanPerformanceResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableLayoutPerformanceResultFilePath",
                                        this.TableLayoutPerformanceResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableExtractPerformanceResultFilePath",
                                        this.TableExtractPerformanceResultFilePath);

            if (this.SrcTgtFieldMappings != null && this.SrcTgtFieldMappings.Count > 0)
            {
                XmlNode mappingRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "FieldMappings");
                foreach (string srcField in this.SrcTgtFieldMappings.Keys)
                {
                    string tgtFieldName = this.SrcTgtFieldMappings[srcField];
                    XmlNode mappingNode = XmlDataUtil.AddElement(ref xDoc, mappingRootNode, "FieldMapping");
                    XmlDataUtil.UpdateAttribute(ref xDoc, mappingNode, "FieldNameInFile", srcField);
                    XmlDataUtil.UpdateAttribute(ref xDoc, mappingNode, "FieldNameInTable", tgtFieldName);
                }
            }

            return xNode;
        }
    }
}
